﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Reflection;

namespace Cn.Cellula.Client {
    public class CtrlUtil {
        public static Common.Logging.ILog log = Common.Logging.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        /// <summary>
        /// 解析控件值，类型为DateTime
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public static DateTime? ParseDateTime(String val) {
            if (String.IsNullOrEmpty(val)) {
                return null;
            } else {
                try {
                    return DateTime.Parse(val);
                } catch {
                    return null;
                }
            }
        }
        public static Int32 ParseInt32(String val) {
            try {
                return Convert.ToInt32(val);
            } catch {
                return 0;
            }
        }
        public static Decimal ParseDecimal(String val) {
            try {
                return Convert.ToDecimal(val);
            } catch {
                return 0;
            }
        }

        public static void BindRddlData<T>(ref TextBox rTxtBox, ref HiddenField rHiddenField, IList<T> rList, String rKeyField, String rTextField) {
            if (rList != null && rList.Count > 0) {
                int count = 0;
                String txt = "";
                String val = "";
                try {
                    foreach (T eo in rList) {
                        if (count > 0) {
                            txt += ",";
                            val += ",";
                        }
                        txt += eo.GetType().GetProperty(rTextField).GetValue(eo, null).ToString();
                        val += eo.GetType().GetProperty(rKeyField).GetValue(eo, null).ToString();
                        count++;
                    }
                    rTxtBox.Text = txt;
                    rHiddenField.Value = val;
                } catch (Exception ex) {
                    log.Error(ex.Message);
                }
            }
        }
    }
}
